package cn.b.sky.mes.pp.controller;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.b.sky.mes.pp.model.ProductionConfirmDetail;
import cn.b.sky.mes.pp.model.condition.ProductionConfirmDetailCondition;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.Material;
import cn.b.sky.mes.cm.model.MaterialType;
import cn.b.sky.mes.cm.model.Operation;
import cn.b.sky.mes.cm.model.condition.MaterialCondition;
import cn.b.sky.mes.cm.model.condition.OperationCondition;
import cn.b.sky.mes.cm.service.MaterialService;
import cn.b.sky.mes.cm.service.MaterialTypeService;
import cn.b.sky.mes.cm.service.OperationService;
import cn.b.sky.mes.cm.service.ProcessService;
import cn.b.sky.common.file.ExcelUtil;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.pp.constants.ProductConstant;
import cn.b.sky.mes.pp.service.OrderTypeService;
import cn.b.sky.mes.pp.service.ProductionConfirmDetailService;

import com.alibaba.fastjson.JSON;

/**
 * <strong>Title : SubmitDetailsController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.controller<br></strong>
 * <strong>Description : </strong>报工明细控制器<br> 
 * <strong>Create on : 2015年10月9日 上午10:20:45<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@Slf4j
@RequestMapping(value = "/confirmDetails/*")
public class ProductionConfirmDetailController extends SecurityController {

  @Resource
  ProductionConfirmDetailService productionConfirmDetailService;

  @Resource
  MaterialService materialService;

  @Resource
  MaterialTypeService materialTypeService;

  @Resource
  OperationService operationService;

  @Resource
  ProcessService processService;

  @Resource
  OrderTypeService orderTypeService;

  @RequestMapping(value = "queryDetails")
  public String queryDetails(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    // 订单类型
    model.addAttribute("orderTypes", JSON.toJSONString(orderTypeService.getAllOrderTypes(this.getSiteCode())));
    return "pp/confirmDetails/confirmDetail_list";

  }

  @RequestMapping(value = "queryDetailList")
  @ResponseBody
  public DWZResult queryDetailList(String navTabId,
                                   @ModelAttribute("condition") ProductionConfirmDetailCondition condition, HttpSession session) {
    condition.setSiteCode(getSiteCode());
    condition.setOrderField("");
    List<ProductionConfirmDetail> prodcutionOrderDetail = productionConfirmDetailService.selectSubmitDetailList(condition);
    int count = productionConfirmDetailService.getSubmitDetailCount(condition);
    condition.setData(prodcutionOrderDetail);
    condition.setTotalCount(count);
    session.setAttribute(ProductConstant.CONFIRMDETAIL_CONDITION, condition);
    return ajaxDone(condition);
  }

  /**
   * 
   * <strong>Title : confirmDetail<br></strong>
   * <strong>Description : </strong>订单归档查询订单报工信息<br> 
   * <strong>Create on : 2016-1-8 下午4:12:18<br></strong>
   * <p>
   * @param condition
   * @param model
   * @param navTabId
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "confirmDetails")
  public String confirmDetail(@ModelAttribute("condition") ProductionConfirmDetailCondition condition, Model model,
      String navTabId) {
    if (condition != null && !StringUtils.isBlank(condition.getProductionNum())) {
      //-1表示全部
      if ("-1".equals(condition.getWorkCenterCodeStr())) {
        condition.setWorkCenterCodeStr(null);
      }
      condition.setSiteCode(this.getSiteCode());
      List<ProductionConfirmDetail> prodcutionOrderDetail = productionConfirmDetailService
          .selectSubmitDetailList(condition);
      int count = productionConfirmDetailService.getSubmitDetailCount(condition);
      List<ProductionConfirmDetail> confirmDetails = productionConfirmDetailService
          .selecPOConfirmDetailWorkCenters(condition);
      condition.setData(prodcutionOrderDetail);
      condition.setTotalCount(count);
      model.addAttribute("confirmDetails", confirmDetails);
      model.addAttribute("page", condition);
    }
    return "/pp/archives/confirmDetail";
  }

  @RequestMapping(value = "productionOrderControlConfirm")
  public String productionOrderControlConfirm(@ModelAttribute("condition") ProductionConfirmDetailCondition condition, Model model,
      String navTabId) {
    if (condition != null && !StringUtils.isBlank(condition.getControlNum())) {
      
      condition.setSiteCode(this.getSiteCode());
      List<ProductionConfirmDetail> prodcutionOrderDetail = productionConfirmDetailService
          .selectSubmitDetailList(condition);
      int count = productionConfirmDetailService.getSubmitDetailCount(condition);
      List<ProductionConfirmDetail> confirmDetails = productionConfirmDetailService
          .selecPOConfirmDetailWorkCenters(condition);
      condition.setData(prodcutionOrderDetail);
      condition.setTotalCount(count);
      model.addAttribute("confirmDetails", confirmDetails);
      model.addAttribute("page", condition);
    }
    return "/pp/productionControlArchives/confirmDetail";
  }
  
  @RequestMapping(value = "controlConfirmDetails")
  public String controlConfirmDetails(ProductionConfirmDetailCondition condition, Model model,
      String navTabId) {
    if (condition != null && !StringUtils.isBlank(condition.getControlNum())) {
     
      condition.setSiteCode(this.getSiteCode());
      List<ProductionConfirmDetail> prodcutionOrderDetail = productionConfirmDetailService
          .selectSubmitDetailList(condition);
      int count = productionConfirmDetailService.getSubmitDetailCount(condition);
      condition.setData(prodcutionOrderDetail);
      condition.setTotalCount(count);
      model.addAttribute("page", condition);
      model.addAttribute("productionControlNum", condition.getControlNum());
    }
    return "/pp/productionControlArchives/confirmDetail";
  }
  
  /**
   * 
   * <strong>Title : confirmDetailOne<br></strong>
   * <strong>Description : </strong>点击工作中心局部刷新统计信息<br> 
   * <strong>Create on : 2016-1-12 下午4:37:36<br></strong>
   * <p>
   * @param condition
   * @param model
   * @param navTabId
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */

  @RequestMapping(value = "confirmDetails_one")
  public String confirmDetailOne(@ModelAttribute("condition") ProductionConfirmDetailCondition condition, Model model,
      String navTabId) {
    if (condition != null && !StringUtils.isBlank(condition.getProductionNum())) {
      //-1表示全部
      if ("-1".equals(condition.getWorkCenterCodeStr())) {
        condition.setWorkCenterCodeStr(null);
      }
      condition.setSiteCode(this.getSiteCode());
      List<ProductionConfirmDetail> confirmDetails = productionConfirmDetailService
          .selecPOConfirmDetailWorkCenters(condition);
      model.addAttribute("confirmDetails", confirmDetails);
    }
    return "/pp/archives/confirmDetail_one";
  }

  /**
   * 
   * <strong>Title : confirmDetailTwo<br></strong>
   * <strong>Description : </strong>局部刷新报工信息<br> 
   * <strong>Create on : 2016-1-12 下午4:38:23<br></strong>
   * <p>
   * @param condition
   * @param model
   * @param navTabId
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "confirmDetails_two")
  public String confirmDetailTwo(@ModelAttribute("condition") ProductionConfirmDetailCondition condition, Model model,
      String navTabId) {
    if (condition != null && !StringUtils.isBlank(condition.getProductionNum())) {
      //-1表示全部
      if ("-1".equals(condition.getWorkCenterCodeStr())) {
        condition.setWorkCenterCodeStr(null);
      }
      condition.setSiteCode(this.getSiteCode());
      List<ProductionConfirmDetail> prodcutionOrderDetail = productionConfirmDetailService
          .selectSubmitDetailList(condition);
      int count = productionConfirmDetailService.getSubmitDetailCount(condition);
      condition.setData(prodcutionOrderDetail);
      condition.setTotalCount(count);
      model.addAttribute("page", condition);
    }
    return "/pp/archives/confirmDetail_two";
  }

  @RequestMapping("selectMaterialList")
  public String numberRuleList(Model model, @ModelAttribute("condition") MaterialCondition material, String navTabId) {
    material.setSiteCode(SecurityContextUtil.getSiteCode());
    List<MaterialType> types = materialTypeService.selectAllUnLockeMaterialTypes(SecurityContextUtil.getSiteCode());
    model.addAttribute("materialTypes", types);
    List<Material> materialList = materialService.selectMaterialList(material);
    int count = materialService.selectMaterialListCount(material);
    material.setData(materialList);
    material.setTotalCount(count);
    model.addAttribute("page", material);
    model.addAttribute(NAVTABID, navTabId);
    return "pp/confirmDetails/material_select";
  }

  @RequestMapping(value = "operationList")
  public String operationList(Model model, @ModelAttribute("condition") OperationCondition operationCondition,
      String navTabId) {
    operationCondition.setSiteCode(this.getSiteCode());
    List<Operation> operations = operationService.selectOperationList(operationCondition);
    int count = operationService.getOperationCount(operationCondition);
    operationCondition.setData(operations);
    operationCondition.setTotalCount(count);
    model.addAttribute("page", operationCondition);
    model.addAttribute(NAVTABID, navTabId);
    return "pp/confirmDetails/operation_select";
  }

  @RequestMapping(value = "exportToExcel")
  public void exportToExcel(HttpServletResponse response, String navTabId,
      @ModelAttribute("condition") ProductionConfirmDetailCondition condition, Model model, HttpSession session) {
    ProductionConfirmDetailCondition condition0 = (ProductionConfirmDetailCondition) session.getAttribute(ProductConstant.CONFIRMDETAIL_CONDITION);
    if (condition0 != null) {
      condition = condition0;
    }
    Map<String, String> map = new LinkedHashMap<String, String>();
    String languageCode = SecurityContextUtil.getLanguageCode();
    
    map.put("productionControlNum", getImeViewMessageI18n(languageCode, "productionControlNum.C"));
    map.put("ProductionOrderNum", getImeViewMessageI18n(languageCode, "productionOrderNum.C"));
    map.put("materialCode", getImeViewMessageI18n(languageCode, "materialCode.C"));
    map.put("materialText", getImeViewMessageI18n(languageCode, "materialText.C"));
    map.put("quantity", getImeViewMessageI18n(languageCode, "quantity.C"));
    map.put("completedQuantity", getImeViewMessageI18n(languageCode, "completedQuantity.C"));
    map.put("scrappedQuantity", getImeViewMessageI18n(languageCode, "scrappedQuantity.C"));
    map.put("failureRate", getImeViewMessageI18n(languageCode, "defectiveRate.C"));
    map.put("confirmUser", getImeViewMessageI18n(languageCode, "confirmUser.C"));
    map.put("confirmDateTime", getImeViewMessageI18n(languageCode, "confirmDateTime.C"));
    map.put("workCenterText", getImeViewMessageI18n(languageCode, "workCenter.C"));
    map.put("workUnitText", getImeViewMessageI18n(languageCode, "workUnit.C"));
    map.put("processOperationId", getImeViewMessageI18n(languageCode, "processOperation.C"));
    map.put("operationCode", getImeViewMessageI18n(languageCode, "operation.C"));
    map.put("orderTypeCode", getImeViewMessageI18n(languageCode, "orderTypeCode.C"));
    map.put("orderTypeText", getImeViewMessageI18n(languageCode, "orderTypeText.C"));
    map.put("customer", getImeViewMessageI18n(languageCode, "customer.C"));
    map.put("customerOrder", getImeViewMessageI18n(languageCode, "customerOrder.C"));
    map.put("priority", getImeViewMessageI18n(languageCode, "priority.C"));
    
    List<ProductionConfirmDetail> dataList = productionConfirmDetailService.selectSubmitDetailList(condition);
    for (ProductionConfirmDetail detail : dataList) {
      detail.setFailureRate(detail.getFailureRate());
    }
    try {
      ExcelUtil.exportToExcel(response, getImeViewMessageI18n(languageCode, "confirmReportDetail.C"), map, dataList);
    } catch (IOException e) {
      log.error("{}", e);
    }

  }

}
